package 算法.动态规划;

/**
 * @author Zhu
 * @Description
 * @create 2023-03-13
 */
public class JZ10斐波那契数列 {
    public int Fibonacci(int n) {
        if(n<=2)
            return 1;
        return Fibonacci(n-1)+Fibonacci(n-2);
    }

    //动态规划 O(n) O(1)
    public int Fibonacci2(int n) {
        if(n<=1)
            return n;
        int res = 0;
        int a = 0;
        int b = 1;
        for (int i = 2; i <= n; i++){
            res = a + b;
            a = b;
            b = res;
        }
        return res;
    }

    public int Fibonacci3(int n) {
        if(n <= 1)
            return n;
        int[] fib = new int[n + 1];
        fib[0] = 0;
        fib[1] = 1;
        //依次相加
        for (int i = 2; i <= n; i++)
            fib[i] = fib[i - 1] + fib[i - 2];
        return fib[n];
    }

}
